<?php
/**
 * 店铺冻结列表
 *
 * @access public
 * @author wangbiao<wangbiao@shopex.cn>
 * @version $Id: shopfreeze.php 2016-03-18 10:00
 */
class console_ctl_admin_shopfreeze extends desktop_controller
{
    public function index()
    {
        $this->title = '店铺冻结明细查询';
        
        $params = array(
            'title' => $this->title,
            'finder_cols' => '*',
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>false,
            'orderBy' => 'sm_id DESC',
        );
        
        $this->finder('console_mdl_shopfreeze', $params);
    }
    
    public function show_shopfreeze_list()
    {
        $sm_id      = intval($_GET['sm_id']);
        $shop_id    = $_GET['shop_id'];
        
        if(empty($sm_id) || empty($shop_id))
        {
            header("content-type:text/html; charset=utf-8");
            echo "<script>alert('无效操作，请检查');window.close();</script>";
            exit;
        }
        
        $statusObj           = kernel::single('ome_order_status');
        $shopObj             = app::get('ome')->model('shop');
        
        #店铺信息
        $shop_info    = $shopObj->dump(array('shop_id'=>$shop_id), 'name');
        
        #货品信息
        $salesObj     = app::get('material')->model('sales_material');
        $product_info = $salesObj->dump(array('sm_id'=>$sm_id), 'sales_material_bn');
        $shop_info['product_bn']    = $product_info['sales_material_bn'];
        
        $this->pagedata['shop_info'] = $shop_info;
        
        #订单信息
        $order_list  = array();
        $order_list  = $this->get_order_info($shop_id, $sm_id);
        $this->pagedata['rows']    = $order_list;
        
        #手工重新计算货品对应的店铺冻结数量
        $shop_freeze_num        = 0;
        $salesMShopFreezeLib    = kernel::single('material_sales_material_shop_freeze');
        $shop_freeze_num        = $salesMShopFreezeLib->update_sales_material_shop_freeze($shop_id, $sm_id);
        
        $this->pagedata['shop_freeze_num']    = $shop_freeze_num;
        
        /**
         * 显示o2o门店未确认收货的订单
         * 注：o2o门店订单收货后才释放店铺冻结数量
         */
        if(app::get('o2o')->is_installed())
        {
            #订单信息
            $order_list  = array();
            $order_list  = $this->get_order_info($shop_id, $sm_id, true);
            
            $this->pagedata['o2o_rows']    = $order_list;
        }
        
        $this->singlepage('admin/stock/shop_freeze_list.html');
    }
    
    /*
     * 获取销售物料已发货数量和部分拆分数量
     */
    public function get_order_item_info($order_id, $obj_id, $process_status=Null)
    {
        $result    = array('send_num'=>0, 'dly_num'=>0, 'delete'=>false);
        
        if(empty($order_id) || empty($obj_id))
        {
            return $result;
        }
        
        $orderObj    = app::get('ome')->model('orders');
        $sql         = "SELECT a.item_id, a.obj_id, a.product_id, a.nums, a.sendnum, a.item_type, b.quantity 
                        FROM sdb_ome_order_items AS a LEFT JOIN sdb_ome_order_objects AS b ON a.obj_id=b.obj_id 
                        WHERE a.order_id=". $order_id ." AND a.obj_id=". $obj_id ." AND a.`delete`='false'";
        $row         = $orderObj->db->selectRow($sql);
        if(empty($row))
        {
            $result['delete']    = true;
            return $result;
        }
        
        #已发货数量
        $result['send_num']    = intval($row['sendnum'] / ($row['nums'] / $row['quantity']));
        
        #部分拆分订单_计算已审核的数量
        if($process_status == 'splitting')
        {
            $dly_sql	= "SELECT SUM(number) AS num FROM `sdb_ome_delivery_items_detail` AS did 
					           JOIN `sdb_ome_delivery` AS d ON d.delivery_id=did.delivery_id 
	                           WHERE did.order_item_id='". $row['item_id'] ."' AND did.product_id='". $row['product_id'] ."' 
	                           AND d.status NOT IN('back', 'cancel', 'return_back') AND d.is_bind='false'";
            $deliveryNum	 = $orderObj->db->selectrow($dly_sql);
            $result['dly_num']    = intval($deliveryNum['num'] / ($row['nums'] / $row['quantity']));
        }
        
        return $result;
    }
    
    /**
     * 获取订单详情
     * 
     * @param $shop_id
     * @param $sm_id
     * @param $is_o2o 查询门店订单
     * 
     * @return Array
     */
    public function get_order_info($shop_id, $sm_id, $is_o2o=false)
    {
        $statusObj           = kernel::single('ome_order_status');
        $shopObj             = app::get('ome')->model('shop');
        
        if($is_o2o)
        {
            $o2o_corp_ids   = array();
            $corpObj        = app::get('ome')->model('dly_corp');
            $corp_info      = $corpObj->getList('corp_id', array('d_type'=>2));
            foreach ($corp_info as $key => $val)
            {
                $o2o_corp_ids[]    = $val['corp_id'];
            }
            
            $sql		= "SELECT o.order_id, o.order_bn, o.process_status, o.ship_status, o.confirm, o.shop_id, o.createtime, 
                           j.obj_id, j.obj_type, j.quantity 
    			           FROM sdb_ome_order_objects AS j LEFT JOIN sdb_ome_orders AS o ON j.order_id=o.order_id 
    	                   WHERE o.process_status='splited' AND o.ship_status='0' 
    			           AND o.status='active' AND o.shop_id='". $shop_id ."' AND j.goods_id=". $sm_id ." AND o.logi_id in(". implode(',', $o2o_corp_ids) .")";
        }
        else 
        {
            $sql		= "SELECT o.order_id, o.order_bn, o.process_status, o.ship_status, o.confirm, o.shop_id, o.createtime,
                           j.obj_id, j.obj_type, j.quantity
    			           FROM sdb_ome_order_objects AS j LEFT JOIN sdb_ome_orders AS o ON j.order_id=o.order_id
    	                   WHERE o.process_status in ('unconfirmed', 'is_retrial', 'is_declare', 'splitting') AND o.ship_status in('0', '2')
    			           AND o.status='active' AND o.shop_id='". $shop_id ."' AND j.goods_id=".$sm_id;
        }
        
        #订单详情
        $order_list  = array();
        $dataList	 = $shopObj->db->select($sql);
        foreach($dataList as $k=>$v)
        {
            $order_id    = $v['order_id'];
            
            $v['status']        = $statusObj->ship_status($v['ship_status']);
            $v['createtime']    = date("Y-m-d H:i:s",$v['createtime']);
            $v['pay_status']    = $statusObj->pay_status($v['pay_status']);
            $v['process_title'] = $statusObj->process_status($v['process_status']);
            
            #购买数量
            $v['nums']       = $v['quantity'] + intval($order_list[$order_id]['nums']);
            
            #已发货数量和部分拆分数量
            $item_info    = $this->get_order_item_info($order_id, $v['obj_id'], $v['process_status']);
            if($item_info['delete'])
            {
                continue;
            }
            
            $v['sendnum']    = $item_info['send_num'] + intval($order_list[$order_id]['sendnum']);
            $v['dly_num']    = intval($item_info['dly_num']) + intval($order_list[$order_id]['dly_num']);
            
            $order_list[$order_id]    = $v;
        }
        
        return $order_list;
    }
}
